package com.itany.service.impl;

import com.itany.entity.Annex;
import com.itany.entity.AnnexExample;
import com.itany.entity.User;
import com.itany.entity.UserExample;
import com.itany.mapper.AnnexMapper;
import com.itany.mapper.UserMapper;
import com.itany.service.UserService;
import com.itany.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper mapper;
    @Autowired
    private AnnexMapper annexMapper;

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Map<String,Object> findById(Integer id) {
        Map<String,Object> map = new HashMap<>();
        User user = mapper.selectById(id);
        map.put("name",user.getName());
        map.put("nickname",user.getNickname());
        map.put("phone",user.getPhone());
        map.put("wechat",user.getWechat());
        map.put("qq",user.getQq());
        map.put("serverCompany",user.getServerCompany());
        map.put("sex",user.getSex());
        map.put("createdate",user.getCreatedate());
        map.put("flag",user.getFlag());
        AnnexExample annexExample = new AnnexExample();
        annexExample.or().andKeyidEqualTo(id).andTypeEqualTo("4").andTabletypeEqualTo(4);
        List<Annex> annexList = annexMapper.selectByExample(annexExample);
        if(!annexList.isEmpty()){
            Annex annex = annexList.get(0);
            map.put("imageUrl",annex.getPath() + annex.getFilename());
        }else {
            map.put("imageUrl", "");
        }
        return map;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void modifyStatus(Integer id) {
        User user = mapper.selectByPrimaryKey(id);
        if(user.getFlag() == 1){
            user.setFlag(0);
        }else{
            user.setFlag(1);
        }
        mapper.updateByPrimaryKeySelective(user);
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Map<String, Object>> queryByParams(String name, String phone, String wechat, String qq, Integer type,String companyid) {
        List<User> list = mapper.selectByParams(name,phone,wechat,qq,type,companyid);
        List<Map<String, Object>> mapList = new ArrayList<>();
        if(!list.isEmpty()) {
            for (User user : list) {
                Map<String, Object> u = new HashMap<>();
                u.put("id",user.getId());
                u.put("name",user.getName());
                u.put("phone",user.getPhone());
                u.put("wechat",user.getWechat());
                u.put("qq",user.getQq());
                u.put("serverCompany",user.getServerCompany());
                u.put("flag",user.getFlag());
                mapList.add(u);
            }
            return mapList;
        }else{
            return null;
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Map<String, Object>> queryByCompanyId(Integer companyId) {
        UserExample example = new UserExample();
        example.or().andCompanyidEqualTo(companyId);
        List<User> list = mapper.selectByExample(example);
        List<Map<String, Object>> mapList = new ArrayList<>();
        if(!list.isEmpty()) {
            for (User user : list) {
                Map<String, Object> u = new HashMap<>();
                u.put("id",user.getId());
                u.put("name",user.getName());
                u.put("phone",user.getPhone());
                u.put("wechat",user.getWechat());
                u.put("qq",user.getQq());
                u.put("flag",user.getFlag());
                mapList.add(u);
            }
            return mapList;
        }else{
            return null;
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void modifyCompanyId(Integer id) {
        mapper.updateCompanyId(id);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public ActionResult addCompanyId(String phone, Integer companyid) {
        UserExample example = new UserExample();
        example.or().andPhoneEqualTo(phone);
        List<User> users = mapper.selectByExample(example);
        if (users.isEmpty()) {
            return ActionResult.success("该用户不存在!", 201, null);
        } else {
            User user = users.get(0);
            if (user.getCompanyid() == null) {
                user.setCompanyid(companyid);
                mapper.updateByPrimaryKeySelective(user);
                return ActionResult.success("绑定成功!", 200, null);
            } else {
                return ActionResult.success("该用户已经关联!", 202, null);
            }
        }
    }
}
